from typing import *


class Solution:
    def maxRemovals(self, source: str, pattern: str, targetIndices: List[int]) -> int:
        st = set(targetIndices)
        m, n = len(source), len(pattern)
        dp = [[-1] * (n + 1) for _ in range(m + 1)]
        dp[0][0] = 0
        for i in range(1, m + 1):
            for j in range(n + 1):
                dp[i][j] = dp[i - 1][j]
                s, p = source[i - 1], pattern[j - 1] if j > 0 else None
                if s == p:
                    dp[i][j] = max(dp[i][j], dp[i - 1][j - 1])
                if (i - 1) in st and dp[i - 1][j] > -1:
                    dp[i][j] = max(dp[i][j], dp[i - 1][j] + 1)

        return dp[m][n]


so = Solution()
print(so.maxRemovals(source="bcda", pattern="d", targetIndices=[0, 3]))
print(so.maxRemovals(source="dda", pattern="dda", targetIndices=[0, 1, 2]))
print(so.maxRemovals(source="yeyeykyded", pattern="yeyyd", targetIndices=[0, 2, 3, 4]))
print(so.maxRemovals(source="abbaa", pattern="aba", targetIndices=[0, 1, 2]))


from random import randint

c = []
for i in range(100):
    c.append(chr(randint(0, 25) + ord("a")))
print("".join(c))
s = "zfpaekagnsmhydwoufxutqawvlgizrbkjabvghkzzgbmhdqmtvfrpzacpvdrzbswjfjaijpovlkwfdqnwglvyyoooawasmbzowyn"
sb = ""
sa = []
for i in range(100):
    r = randint(0, 5)
    if r == 3:
        # sb += s[i]
        sa.append(i)
print(sa)
# fvmqw
